package com.mofeng.myshop.service.impl;

import com.mofeng.myshop.domain.dto.SettingDto;
import com.mofeng.myshop.domain.po.Setting;
import com.mofeng.myshop.domain.vo.SettingVo;
import com.mofeng.myshop.mapper.SettingMapper;
import com.mofeng.myshop.service.ISettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mofeng.myshop.until.Constants;
import com.mofeng.myshop.until.MyUtil;
import com.mofeng.myshop.until.R;
import com.mofeng.myshop.until.TimeOutUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站基础设置 服务实现类
 * </p>
 *
 * @author MoFeng
 * @since 2024-04-14
 */
@Service
@RequiredArgsConstructor
public class SettingServiceImpl implements ISettingService {

    private final SettingMapper settingMapper;

    /**
     * 管理员获取设置列表
     */
    @Override
    public R getListAdmin() {
        List<Setting> settingList = settingMapper.findAll();
        for (int i = 0; i < settingList.size(); i++) {
            Setting setting = settingList.get(i);
            if (Constants.Settings.MANAGER_ACCOUNT_INIT_STATE.equals(setting.getKey())) {
                settingList.remove(setting);
                break;
            }
        }
        return R.SUCCESS().setData(SettingVo.change(settingList));
    }

    /**
     * 获取系统有效期
     */
    @Override
    public R getSystemTime() {
        TimeOutUtil.isTimeOut();
        return R.SUCCESS();
    }

    /**
     * 修改
     */
    @Override
    public R updateSetting(SettingDto settingDto) {
        Setting setting = settingDto.toSetting();
        Setting settingFromDb = settingMapper.selectById(setting.getId());
        if (settingFromDb == null) {
            return R.FAILED("找不到此配置信息");
        }
        settingMapper.updateById(setting);
        return R.SUCCESS("网站信息修改成功").setData(new SettingVo(setting));
    }

    /**
     * 删除配置
     *
     * @param ids
     */
    @Override
    public R delSetting(List<String> ids) {
        List<Long> idList = ids.stream().map(Long::parseLong).toList();
        settingMapper.deleteBatchIds(idList);
        return R.SUCCESS();
    }

    /**
     * 获取网站基础信息
     */
    @Override
    public R getWebInfo() {
        List<Setting> settingList = settingMapper.findAll();
        Map<String, String> map = new HashMap<>();
        for (Setting setting : settingList) {
            if (Constants.Settings.WEB_TITLE.equals(setting.getKey())) {
                map.put(setting.getKey(), setting.getValue());
            }
            if (Constants.Settings.WEB_DESCRIPTION.equals(setting.getKey())) {
                map.put(setting.getKey(), setting.getValue());
            }
            if (Constants.Settings.WEB_KEYWORDS.equals(setting.getKey())) {
                map.put(setting.getKey(), setting.getValue());
            }
            if (Constants.Settings.WEB_LOGO.equals(setting.getKey())) {
                map.put(setting.getKey(), setting.getValue());
            }
            if (Constants.Settings.WEB_DISCOUNT.equals(setting.getKey())) {
                map.put(setting.getKey(), setting.getValue());
            }
        }
        return R.SUCCESS("成功", map);
    }

    @Override
    public Double getDiscount() {
        Setting byKey = settingMapper.findByKey(Constants.Settings.WEB_DISCOUNT);
        Double result = MyUtil.NumberUtil.asDouble(byKey.getValue());
        if (result == null || result <= 0) {
            result = 1.0;
        } else {
            result = result * 0.1;
        }
        return result;
    }
}
